#include <hellomake.h>


#if 1
//#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include <unistd.h>
#include <time.h>

#include <que_struct_type.h>
#include "que_cpu_dis_amp.h"

#if 1
#define TOTAL_SIZE 1024
//#define PACK_SIZE 256
#define PACK_SIZE sizeof(defCpuProtoData)

#define PACK_TAIL_SIZE 8
#define PACK_HEAD1 0x88
#define PACK_HEAD2 0x99
#define PACK_WRONG1 0x11
#define PACK_WRONG2 0x22
#endif
uint8_t timeStamp[3] = {8, 0, 0};
bool get_buf_send_cpu_dis_amp(int index_pack, uint8_t* pbufpara )
{
	uint8_t check_result = 0;
	defCpuProtoData cpu_protocol;
	memset(&cpu_protocol,0x00,sizeof(defCpuProtoData));
	int ncount=0;
	bool bl_last_pack=false;
	int nquelen=cpu_dis_amp_length();

	nquelen= nquelen>defFrameCpuDisAmp?defFrameCpuDisAmp:nquelen;
	cpu_protocol.head88=PACK_HEAD1;
	cpu_protocol.head99=PACK_HEAD2;
	cpu_protocol.seqno=index_pack;    
    cpu_protocol.groupnum=0;
	for( ncount=0;ncount<nquelen; ncount++ )
	//for( ncount=0;ncount<defFrameCpuDisAmp; ncount++ )
	{
        bool blget=cpu_dis_amp_get_index( index_pack*defFrameCpuDisAmp+ncount, &(cpu_protocol.dis_amp[ncount]) );
        if( true == blget )
        {
            cpu_protocol.groupnum++;
        }

		if( false==blget || index_pack*defFrameCpuDisAmp+ncount+1 >=cpu_dis_amp_length()  )
		{
			bl_last_pack=true;
			break;
		}
		//ncount++;
	}
	

	//cpu_protocol.groupnum=ncount;
	//cpu_protocol.groupnum = ncount;//nquelen; // cpu_dis_amp_length();//nquelen;
	if( true == bl_last_pack )
	{
		cpu_protocol.tm.hour=timeStamp[0];
		cpu_protocol.tm.min=timeStamp[1];
		cpu_protocol.tm.sec=timeStamp[2];
		cpu_protocol.lowpower=0x08;//get_adc_val() & 0xFF;
		cpu_protocol.highpower=0x09;//get_adc_val() >> 8;
		cpu_protocol.parkStatus.parkEep=1;//is_nobus_berth_eeprom()?NOTHING:CARPARK;
		cpu_protocol.parkStatus.parkReq=2;//get_req_cpu_soc();
		cpu_protocol.m_nMcuVerSect.byte=0x18;//get_mcu_ver_sect();
	}
	
	//memcpy( LarseDataOne, pbuf, sizeof(defCpuProtoData) );


	uint8_t* pbuf=(uint8_t*)(&cpu_protocol);
	//for (int j = 0; j < PACK_SIZE - 1; j++)
	for (int j = 0; j < offsetof(defCpuProtoData,u8crc); j++)
	{
		check_result ^= pbuf[j];
	}
	cpu_protocol.u8crc=check_result;
	
	memcpy(pbufpara, &cpu_protocol, sizeof(defCpuProtoData) );
	
    //printf("%s\r\n",buf);
			//memcpy( LarseDataOne, buf, sizeof(defCpuProtoData) );
		//HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&(LarseDataOne), sizeof(defCpuProtoData));	

	//memcpy( LarseDataOne, &cpu_protocol, sizeof(defCpuProtoData) );
	//HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&(LarseDataOne), sizeof(defCpuProtoData));	

	return true;

}


void que_test(int ncount_num)
{
    defCpuDisAmp cpu_dis_amp;
    cpu_dis_amp.dis.u8low=ncount_num%0x100;
    cpu_dis_amp.dis.unhigh.byte=ncount_num/0x100;
    
    cpu_dis_amp.amp.u8low=(ncount_num+0x110)%0x100;
    cpu_dis_amp.amp.u8high=(ncount_num+0x110)/0x100;

    for(int ncout_push=0; ncout_push<3; ncout_push++ )
    {
        if( true == cpu_dis_amp_push_back(&cpu_dis_amp) )
            break;
        defCpuDisAmp dis_amp_pop;
        cpu_dis_amp_pop_front( &dis_amp_pop );
        printf("%s;%s;%d;%02X;%02X;%02X;%02X\n",__FILE__,__func__,__LINE__
            ,dis_amp_pop.dis.u8low,dis_amp_pop.dis.unhigh.byte,dis_amp_pop.amp.u8low,dis_amp_pop.amp.u8high);        
        if(ncout_push>=2)
        {
            return;
        }
    }

    uint16_t u16_len=cpu_dis_amp_length();
    printf("====================bgn;u16_len=%d\n",u16_len);
    uint8_t u8_pack_count=u16_len/defFrameCpuDisAmp;
    if( (u16_len%defFrameCpuDisAmp) > 0 )
        u8_pack_count++;
    uint8_t u8_pack_loop;
    for(u8_pack_loop=0; u8_pack_loop<u8_pack_count; u8_pack_loop++)
    {
        usleep(5*1000);
        uint8_t buftmp[PACK_SIZE];
        get_buf_send_cpu_dis_amp(u8_pack_loop,buftmp);  // index_pack=0
        
		char buf[3*PACK_SIZE+10];
		memset(buf,0x00,sizeof(buf));
        int ncount_pack_size;
		for(ncount_pack_size=0;ncount_pack_size<PACK_SIZE;ncount_pack_size++)
		{
			sprintf(&buf[ncount_pack_size*3], "%02X ",buftmp[ncount_pack_size]);
		}
        //printf("count_pack_size=%d\r\n",ncount_pack_size);
        printf("%s\r\n",buf);

        #if 0
        int k=0;
		for(k=0;k<16;k++)
        {
            uint8_t bufprt[16*3+1];
            memset(bufprt,0x00,sizeof(bufprt));
            memcpy(bufprt,&buf[k*(sizeof(bufprt)-1) ],sizeof(bufprt)-1);
            printf("%s\n",bufprt);
            //k++;
        }
        #endif
        //memcpy( LarseDataOne, buf, sizeof(defCpuProtoData) );
        //HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&(LarseDataOne), sizeof(defCpuProtoData));	
    }
    printf("#####################end\n");

}

int getCurTmLogStr( void )
{
    char bufTmFm[32];
    time_t timep;
    time(&timep);
    memset(bufTmFm,0x00,sizeof(bufTmFm));
    strftime( bufTmFm, 19, "%Y-%m-%d;%X",  gmtime( &timep ) );
    printf("%s\n",bufTmFm);
    return 0;
    //return strtm;
}

int getCurTmFnStr(void )
{
    char bufTmFm[32];
    time_t timep;
    time(&timep);
    memset(bufTmFm,0x00,sizeof(bufTmFm));
    //strftime( bufTmFm, 19, "%Y-%m-%d;%X",  gmtime( &timep ) );
    strftime( bufTmFm, 14, "%Y%m%d%H%M%S", gmtime( &timep ) );
    printf("%s\n",bufTmFm);

    strftime( bufTmFm, 14, "%Y%m%d%H%M%S", localtime( &timep ) );
    printf("%s\n",bufTmFm);

    strftime( bufTmFm, 19, "%Y-%m-%d;%H:%M:%S",  localtime( &timep ) );
    printf("%s\n",bufTmFm);

    return 0;
    //return strtm;
}
int main(void)
{
    int count;
    int ntmp;
    int i;
    uint16_t v;
    uint16_t u16val;
    // ./hellomake.out > tt01.txt
    getCurTmLogStr();
    getCurTmFnStr();
    for(i=0;i<29;i++)  // 258//1000
    {
        que_test(i);
    }
    return 0;

#if 0
    //struct u16Queue* pRtcQue = getRtcQuePt();
    defTypeQue* pRtcQue = getRtcQuePt();
    defQueFunc* pQueFunc = (defQueFunc*)get_pt_que_func();
    
    printf("%s,%s,%d,%d\r\n", __FILE__,__func__,__LINE__, get_pt_que_func()->length(pRtcQue) );
    u16val=34;
    pQueFunc->push_back(pRtcQue,&u16val);

    for( i=0; i<21; i++ )
    {
        u16val = i*3+0x3403;
        printf("%s,%s,%d,0x%08X\r\n", __FILE__,__func__,__LINE__, u16val );
        pQueFunc->push_back(pRtcQue,&u16val);
    }
    //return 0;
    #if 1
    count = pQueFunc->length(pRtcQue);
    printf("%s,%s,%d,%d\r\n", __FILE__,__func__,__LINE__, count );
    //return 0;
    for(i=0;i<count;i++)
    {  
        if(pQueFunc->get_index(pRtcQue,i,&v)==false)
            break;
        printf("%s,%s,%d,%d;0x%08X\r\n", __FILE__,__func__,__LINE__, i, v );
    }
    #endif

    void prtRtcQue(void);
    prtRtcQue();

    pQueFunc->pop_front(pRtcQue,&u16val);
    #if 1
    count = pQueFunc->length(pRtcQue);
    printf("%s,%s,%d,%d\r\n", __FILE__,__func__,__LINE__, count );
    //return 0;
    for(i=0;i<count;i++)
    {  
        if(pQueFunc->get_index(pRtcQue,i,&v)==false)
            break;
        printf("%s,%s,%d,%d;0x%08X\r\n", __FILE__,__func__,__LINE__, i, v );
    }
    #endif
    prtRtcQue();
#endif
    return 0;
}
#endif



